package com.hansheng.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hansheng.dao.impl.SheetDaoImpl;
import com.hansheng.entity.*;
import com.hansheng.service.SheetService;

import java.util.List;

/**
 * Created by GoldenMan on 2017/5/12.
 */
public class SheetServiceImpl implements SheetService {
    private SheetDaoImpl sheetDao;

    public void setSheetDao(SheetDaoImpl sheetDao) {
        this.sheetDao = sheetDao;
    }

    public boolean saveSheet(SheetEntity sheetEntity) {
        return sheetDao.saveSheet(sheetEntity);
    }

    public boolean saveOutForm(OutFormEntity outFormEntity, JSONArray sheetDetails) {
        boolean flag = sheetDao.saveOutForm(outFormEntity);
        for (int i = 0; i < sheetDetails.size(); i++) {
            JSONObject detail = (JSONObject) sheetDetails.get(i);
            // Created by GoldenMan at 2017/5/15    TODO:check availble
            OutDetailEntity outDetailEntity = new OutDetailEntity();
            outDetailEntity.setOutId(outFormEntity.getOutId());
            outDetailEntity.setCutMaterialFormNo(detail.get("cutMaterialFormNo").toString());
            outDetailEntity.setSid(Integer.parseInt(detail.get("sid").toString()));
            outDetailEntity.setLength(Integer.parseInt(detail.get("length").toString()));
            outDetailEntity.setWidth(Integer.parseInt(detail.get("width").toString()));
            outDetailEntity.setHeight(Integer.parseInt(detail.get("height").toString()));
            outDetailEntity.setCount(Integer.parseInt(detail.get("count").toString()));
            outDetailEntity.setWeightThe(Double.parseDouble(detail.get("weightThe").toString()));
            outDetailEntity.setWeightAct(Double.parseDouble(detail.get("weightAct").toString()));
            outDetailEntity.setStatus(3);
            flag &= sheetDao.saveOutDetail(outDetailEntity);
        }
        return flag;
    }

    @Override
    public boolean saveTailings(int outId, int operatorId, String tailingDetailStr) {
        JSONArray tailingDetails = JSONArray.parseArray(tailingDetailStr);
        boolean flag = sheetDao.changeOldSheetStatus(outId);
        TailingEntity tailingEntity = new TailingEntity();
        tailingEntity.setOutId(outId);
        tailingEntity.setOperator(operatorId);
        tailingEntity.setStatus(1);
        flag &= sheetDao.saveTailing(tailingEntity);
        for (int i = 0; i < tailingDetails.size(); i++) {
            JSONObject jsonObject = (JSONObject) tailingDetails.get(i);
            TailingDetailEntity tailingDetailEntity = new TailingDetailEntity();
            tailingDetailEntity.setTailingId(tailingEntity.getTailingId());
            tailingDetailEntity.setMsid(Integer.parseInt(jsonObject.get("msid").toString()));
            tailingDetailEntity.setLength(Integer.parseInt(jsonObject.get("length").toString()));
            tailingDetailEntity.setWidth(Integer.parseInt(jsonObject.get("width").toString()));
            tailingDetailEntity.setHeight(Integer.parseInt(jsonObject.get("height").toString()));
            tailingDetailEntity.setCount(Integer.parseInt(jsonObject.get("count").toString()));
            tailingDetailEntity.setWeightThe(Double.parseDouble(jsonObject.get("weightThe").toString()));
            tailingDetailEntity.setWeightAct(Double.parseDouble(jsonObject.get("weightAct").toString()));
            tailingDetailEntity.setStatus(1);
            flag &= sheetDao.saveTailingDetail(tailingDetailEntity);
            SheetEntity sheetEntity = new SheetEntity();
            sheetEntity.setMsid(Integer.parseInt(jsonObject.get("msid").toString()));
            sheetEntity.setOperator(operatorId);
            sheetEntity.setLength(Integer.parseInt(jsonObject.get("length").toString()));
            sheetEntity.setWidth(Integer.parseInt(jsonObject.get("width").toString()));
            sheetEntity.setHeight(Integer.parseInt(jsonObject.get("height").toString()));
            sheetEntity.setCount(Integer.parseInt(jsonObject.get("count").toString()));
            sheetEntity.setWeightThe(Double.parseDouble(jsonObject.get("weightThe").toString()));
            sheetEntity.setWeightAct(Double.parseDouble(jsonObject.get("weightAct").toString()));
            sheetEntity.setStatus(1);
            flag &= sheetDao.saveSheet(sheetEntity);
        }
        return flag;
    }


    @Override
    public JSONArray getSheetByCondition(String whereHql, Object[] params, int firstResult, int maxResult) {
        List list = sheetDao.getSheetListByCondition(whereHql, params, firstResult, maxResult);
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            VSheetEntity vSheetEntity = (VSheetEntity) list.get(i);
            JSONObject tempJSON = new JSONObject();
            tempJSON.put("sid", vSheetEntity.getSid());
            tempJSON.put("msNo", vSheetEntity.getMsNo());
            tempJSON.put("material", vSheetEntity.getMaterial());
            tempJSON.put("length", vSheetEntity.getLength());
            tempJSON.put("width", vSheetEntity.getWidth());
            tempJSON.put("height", vSheetEntity.getHeight());
            tempJSON.put("count", vSheetEntity.getCount());
            tempJSON.put("weightThe", vSheetEntity.getWeightThe());
            tempJSON.put("weightAct", vSheetEntity.getWeightAct());
            tempJSON.put("addTime", vSheetEntity.getAddTime());
            tempJSON.put("status", vSheetEntity.getStatus());
            tempJSON.put("msid", vSheetEntity.getMsNo());
            tempJSON.put("oid", vSheetEntity.getOid());
            tempJSON.put("name", vSheetEntity.getName());
            tempJSON.put("position", vSheetEntity.getPosition());
            tempJSON.put("mid", vSheetEntity.getMid());
            jsonArray.add(tempJSON);
        }
        return jsonArray;
    }

    @Override
    public JSONArray getAllOutForm() {
        List list = sheetDao.getAllOutForm();
        JSONArray returnArray = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            returnArray.add(list.get(i));
        }
        return returnArray;
    }

    @Override
    public int getOutFormNoCounter(String cutMaterialNo) {
        String hql = "from OutFormEntity o where o.outFormNo LIKE ? order by o.outId desc";
        Object[] params = {cutMaterialNo + "%"};
        List<OutFormEntity> outFormEntityList = sheetDao.getOutFormByQuery(hql, params);
        int counter = Integer.parseInt(outFormEntityList.get(0).getOutFormNo().substring(cutMaterialNo.length())) + 1;
        return counter;
    }

    @Override
    public JSONArray getOutFormArrayByCondition(String whereHql, Object[] params) {
        List<OutFormEntity> list = sheetDao.getOutFormListByCondition(whereHql, params);
        JSONArray returnArray = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            returnArray.add(list.get(i));
        }
        return returnArray;
    }

}
